package cn.itcast.service.impl;

import cn.itcast.dao.Company;
import cn.itcast.dao.Dept;
import cn.itcast.factory.MapperFactory;
import cn.itcast.mapper.DeptMapper;
import cn.itcast.service.DeptService;
import cn.itcast.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class DeptServiceImpl implements DeptService {
    private SqlSession sqlSession = null;
    private DeptMapper mapper = null;
    @Override
    public List<Dept> findAll(int pageNum) {
        sqlSession = MapperFactory.getSqlSession();
        mapper = MapperFactory.getMapper(sqlSession, DeptMapper.class);
        PageHelper.startPage(pageNum,10);
        List<Dept> list = mapper.findAll();
        TransactionUtil.close(sqlSession);
        return list;
    }

    @Override
    public Dept findById(String id) {
        sqlSession = MapperFactory.getSqlSession();
        mapper = MapperFactory.getMapper(sqlSession, DeptMapper.class);
        Dept dept = mapper.findById(id);
        TransactionUtil.close(sqlSession);
        return dept;
    }

    @Override
    public void edit(Dept dept) {
        sqlSession = MapperFactory.getSqlSession();
        mapper = MapperFactory.getMapper(sqlSession, DeptMapper.class);
        mapper.edit(dept);
        TransactionUtil.commit(sqlSession);
        TransactionUtil.close(sqlSession);
    }

}
